/**
 *  Copyright Matt Silbernagel 2010
 *  
 *	This file is part of MobilePrinting.
 *
 *	MobilePrinting is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MobilePrinting is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MobilePrinting.  If not, see <http://www.gnu.org/licenses/>.
 */
package edu.uc.labs.MobilePrinting.SocketClient;

import java.net.*;
import java.awt.TrayIcon;
import java.io.*;

import edu.uc.labs.MobilePrinting.GUI.IconAnimator;
import edu.uc.labs.MobilePrinting.GUI.SysTray;
import edu.uc.labs.MobilePrinting.PrintUtils.Printer;

import static edu.uc.labs.MobilePrinting.Configurator.*;

/**
 * @author Matt Silbernagel
 *
 */
public class SocketClient {
	
	Socket client;
	BufferedReader in;
	PrintWriter out;
	
	String servername = "localhost";
	
	public SocketClient(){
		try {
			client = new Socket(servername, DaemonPort);
			
		} catch (UnknownHostException e) {
			LOGGER.error( "Unable to connect to " + servername );
			e.printStackTrace();
		} catch (IOException e) {
			LOGGER.error( "IOException reached while trying to connect to the Server process");
			e.printStackTrace();
		}
		
	}
	
	public String sendCommand( String command, Printer printer ){
		
		// Change the icon to the progress icon
		IconAnimator progress = new IconAnimator();
		progress.start();
		
		String response = null;
		
		try {
			LOGGER.debug("Setting up Reader and Writer streams");
			in = new BufferedReader
				(new InputStreamReader(client.getInputStream()));
			out = new PrintWriter
			  	(client.getOutputStream(), true /* autoFlush */);
		} catch (IOException e) {
			LOGGER.error("Could not create an Reader or Writer stream");
			e.printStackTrace();
			return response;
		}
		
		
		//SysTray.setProgIcon();
		SysTray.sendMessage("Installing","Please wait while the printer, " + printer.getName() + ", is being installed", TrayIcon.MessageType.NONE);

		
		
		
		return response;
	}
	

}
